<!-- 菜单悬浮的原理: 通过给菜单添加position:sticky实现, 用法超简单, 仅APP端的低端机不兼容 https://caniuse.com/#feat=css-sticky -->
<template>
	<view>
		<Imitate ref="imitate" v-if="pageType === 2"></Imitate>
		<FirmOffer v-else @switchPage="switchPage"></FirmOffer>
		<uni-tabbar-custom :current="2"></uni-tabbar-custom>
	</view>
</template>

<script>
	import Imitate from "./imitate.vue"
	import FirmOffer from "./firmOffer.vue"
	import Socket from '@/utils/ws.js'
	export default {
		components: {
			Imitate,
			FirmOffer
		},
		data() {
			return {
				pageType: 2,
			}
		},
		onLoad(e) {
			uni.hideTabBar({})
			// this.page()
			var that = this;
			that.$nextTick(() => {
				let data = (uni.getStorageSync('chicang') && JSON.parse(uni.getStorageSync(
					'chicang'))) || [];
				let data1 = (uni.getStorageSync('quanyi') && JSON.parse(uni.getStorageSync(
					'quanyi'))) || [];
				let data2 = (uni.getStorageSync('order') && JSON.parse(uni.getStorageSync(
					'order'))) || [];
				let data3 = (uni.getStorageSync('trade') && JSON.parse(uni.getStorageSync(
					'trade'))) || [];

				that.$refs.imitate.chooseQY({
					data3: data3,
					data2: data2,
					data1: data1,
					data: data
				})

			})

			if (!uni.getStorageSync('td')) {
				uni.navigateTo({
					url: '/pages/logon/logon/logon'
				})
			} else {
				// 发送数据
				// Socket.socketTask['login'].send({
				// 	data: ('L|' + td),
				// 	success: () => {
				// 		console.log("发送websocket中...");

				// 	},
				// 	fail: (err) => {

				// 	}

				// })
				this.$nextTick(() => {

					Socket.socketTask['login'].onMessage((res) => {
						var arr = res.data.split('|');
						console.log(arr, '====>arr0000000')
						var type = arr[0];
						if (type == "M") //权益
						{
							var amountInfo = JSON.parse(arr[1]);
							console.log(amountInfo, '===>权益')
						} else if (type == "P") //持仓
						{
							var poInfo = JSON.parse(arr[1]);
							console.log(poInfo, '===>持仓', )
							that.$nextTick(() => {
								let data = (uni.getStorageSync('chicang') && JSON.parse(uni
									.getStorageSync(
										'chicang'))) || [];
								let data1 = (uni.getStorageSync('quanyi') && JSON.parse(uni
									.getStorageSync(
										'quanyi'))) || [];
								let data2 = (uni.getStorageSync('order') && JSON.parse(uni
									.getStorageSync(
										'order'))) || [];
								let data3 = (uni.getStorageSync('trade') && JSON.parse(uni
									.getStorageSync(
										'trade'))) || [];
								that.$refs.imitate.chooseQY({
									data3: data3,
									data2: data2,
									data1: data1,
									data: data
								})
							})
						}
					})
				})
			}
		},
		onShow() {

		},
		methods: {
			page() {
				uni.connectSocket({
					url: 'wss://socket.salefx.cn:39730',
					success: () => {
						console.log("正准备建立websocket中...");
						// 返回实例
						uni.onSocketOpen(function() {
							console.log('WebSocket连接已打开');
							uni.sendSocketMessage({
								data: (JSON.stringify({
									key: 'qdBMTaBwQi2ukKGmdfvipFASmT',
									quote: 'rb2405',
								})),
								success() {
									console.log('消息发送成功');
								},
								fail(err) {
									console.error('消息发送失败', err);
								}
							});
						});

						uni.onSocketMessage(function(res) {
							console.log('收到消息：====>', res.data);
						});

						uni.onSocketError(function(err) {
							console.error('WebSocket错误：', err);
						});
					}
				});
			},
			switchPage(i) {
				this.pageType = i
				if (i === 2) {
					uni.setNavigationBarColor({
						frontColor: '#ffffff',
						backgroundColor: '#EE5953',
					})
				} else {
					uni.setNavigationBarColor({
						frontColor: '#000000',
						backgroundColor: '#ffffff',
					})
				}
			}
		}
	}
</script>

<style lang="scss">
	page {
		background: #F4F5F7;
	}
</style>